#include "StdAfx.h"
#include "vdblogin.h"
#include "../VSql/VSqlDatabase.h"

VDBLogin::VDBLogin(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);

	connect(ui.pushButtonConnect, SIGNAL(clicked()), this, SLOT(connectToDataBase()));
	connect(ui.pushButtonCancel, SIGNAL(clicked()), this, SLOT(slotCancel()));
}

VDBLogin::~VDBLogin()
{

}

void VDBLogin::connectToDataBase()
{
	VSqlDatabase::setConnectionString(connectionString());
	
	if (VSqlDatabase::checkConnection())
		QDialog::accept();
}

void VDBLogin::slotCancel()
{
	QDialog::accept();
}

bool VDBLogin::isConnected() const
{
	return true;
}

QString VDBLogin::host() const
{
	return ui.lineEditHost->displayText();
}

void VDBLogin::setHost(const QString& host)
{
	ui.lineEditHost->setText(host);
}

QString VDBLogin::database() const
{
	return ui.lineEditDatabase->displayText();
}

void VDBLogin::setDatabase(const QString& db)
{
	ui.lineEditDatabase->setText(db);
}

QString VDBLogin::user() const
{
	return ui.lineEditUser->displayText();
}

void VDBLogin::setUser(const QString& user)
{
	ui.lineEditUser->setText(user);
}

QString VDBLogin::password() const
{
	return ui.lineEditPassword->text();
}

void VDBLogin::setPassword(const QString& password)
{
	ui.lineEditPassword->setText(password);
}

QStringList VDBLogin::connectionString() const
{
	QStringList cs;
	cs << host() << database() << user() << password();
	return cs;
}

void VDBLogin::setConnectionString(const QStringList& conStr)
{
	if (conStr.count() < 4)
		return;

	QStringList cs(conStr);

	setHost(cs.takeFirst());
	setDatabase(cs.takeFirst());
	setUser(cs.takeFirst());
	setPassword(cs.takeFirst());
}